package com.apware.utils.dao;

import java.io.File;
import java.sql.Connection; 

import org.apache.log4j.Logger;

import snaq.db.ConnectionPool;
import snaq.db.ConnectionPoolManager;

public class DbConnectionManager {

	private static ConnectionPoolManager cpm = null;
	static Logger log = Logger.getLogger(DbConnectionManager.class);

	/**
	 * 初始化
	 */
	 static boolean init() {

		try {
			String fileName = "conf/dbpool.properties";
			cpm = ConnectionPoolManager.getInstance(new File(fileName));
			return true;
		} catch (Exception e) {
			log.error("初始化线程池失败！", e);
			return false;
		}
	}

	/**
	 * 获取连接对象
	 * 
	 * @param poolName
	 * @return
	 */
	public static synchronized Connection getConnection(String poolName) {

		try {
			if (cpm == null)
				init();
			ConnectionPool pool = cpm.getPool(poolName);
			if (pool == null) {
				log.error("get connection pool fail");
				return null;
			}

			Connection conn = pool.getConnection();

			if (conn == null)
				log.error("get connection fail");

			return conn;

		} catch (Exception e) {
			log.error("get connection exception", e);
			return null;
		}

	}	 
	
}
